﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HiSportApplet.Application.Common.SqlSugar;
using HiSportApplet.Core.Entity;
using Microsoft.EntityFrameworkCore;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HiSportApplet.Application.Service.Text.Service
{
    public class TestService: ITestService, ITransient
    {

        public readonly ISqlSugarClient _dbContext;
        private readonly SqlSugarRepository<Test1> _test1;
        private readonly SqlSugarRepository<Test2> _test2;
        public TestService(ISqlSugarClient dbContext, SqlSugarRepository<Test1> test1, 
            SqlSugarRepository<Test2> test2)
        {
            _dbContext = dbContext;
            _test1 = test1;
            _test2 = test2;
        }

        /// <summary>
        /// CodeFirst
        /// </summary>
        /// <returns></returns>
        public Task<bool> CodeFirst()
        {


            var types = Assembly.Load("HiSportApplet.Core").GetTypes()
                .Where(x => x.GetCustomAttribute<SugarTable>() != null).ToArray();

            if (types.Count() <= 0)
            {
                throw Oops.Oh("没找到实体");
            }
            //注入
            _dbContext.CodeFirst.InitTables(types);

            //var sql = "";
            //_dbContext .Queryable<Test1>(sql)
            //    .Where(x=>x.Id="")
            //    .ToList();
            return Task.FromResult(true);
        }

        public async Task<bool> Text1()
        {

          

            await _test1.AsInsertable(new Test1()
            {
                str1 = "1",
            }).ExecuteReturnEntityAsync();

            var vv = new Test1();
            vv = null;
            var vvv = vv.str1;
            await _test2.AsInsertable(new Test2()
            {
                str1 = "1",
            }).ExecuteReturnEntityAsync();

            return true;
        }

        public async Task<bool> Text2()
        {
            await _dbContext.Insertable(new Test1()
            {
                str1 = "2",
            }).ExecuteReturnEntityAsync();
            var vv = new Test1();
            vv = null;
            var vvv = vv.str1;
            await _dbContext.Insertable(new Test2()
            {
                str1 = "2",
            }).ExecuteReturnEntityAsync();

            return true;
        }
    }
}
